﻿using System;
using Serilog;
using Serilog.Events;
using Topshelf;

namespace Elasticsearch.Synchronize.Job
{
    class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run(x =>
            {
                Log.Logger = new LoggerConfiguration()
                    .WriteTo.Console(LogEventLevel.Information)
                    .ReadFrom.AppSettings()
                    .CreateLogger();

                x.UseSerilog();

                x.UseAssemblyInfoForServiceInfo();

                x.Service(settings => new JobService(), s =>
                {
                    s.BeforeStartingService(_ => Log.Logger.Debug("BeforeStart"));
                    s.BeforeStoppingService(_ => Log.Logger.Debug("BeforeStop"));
                });

                x.SetStartTimeout(TimeSpan.FromSeconds(10));
                x.SetStopTimeout(TimeSpan.FromSeconds(10));

                x.OnException((exception) => { Log.Logger.Error("Exception thrown - " + exception.Message); });
            });
        }
    }
}